<template>
  <el-dialog
    title="客户意向状态"
    :visible.sync="dialogVisible"
    width="340px"
    append-to-body
    :before-close="handleClose"
    @open='open'
    >
    <el-form>
      <el-form-item label="意向状态">
        <el-select
          v-model="intention"
          class="filter-left"
          placeholder="请选择意向状态"
          filterable
          size="small"
        >
          <el-option v-for="item in options" :label="item.label" :value="item.value"/>
        </el-select>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="handleClose">取 消</el-button>
      <el-button type="primary" @click="setIntention()">确 定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import {getCustomer, setIsIntention} from "@/api/customer";
import {setBlockIsIntention} from "@/api/customer/block";

export default {
  name: "Intention",
  props: {
    show: {
      type: Boolean,
      default: false
    },
    id: '',
    closeAction: '',
    source: {
      type: String,
      default: "customer"
    },
  },
  data() {
    return {
      dialogVisible: this.show,
      intention: 1,
      options: [
        {label: '有', value: 1},
        {label: '无', value: 0},
      ]
    }
  },
  methods: {
    handleClose(done) {
      this.$emit('update:show', false)
    },
    async open(){
      if(typeof this.id != 'object'){
        let {data} = await getCustomer(this.id)
        this.intention = data.isIntention
      }else{
        this.intention = 1
      }
      setTimeout(() => {
        this.infoLoading = false
      }, 0.5 * 1000)
    },
    setIntention() {
      let msg = '确认要修改该客户的意向状态吗？';
      if (typeof this.id != 'object') {
        msg = '确认要修改客户的意向状态吗？'
      }
      this.$confirm(msg, '提示', {
        confirmButtonText: '确认',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        let data = {ids: this.id, value: this.intention}
        if (typeof data.ids != 'object') {
          data.ids = [data.ids]
        }
        if(this.source == 'customer'){
          await setIsIntention(data).then((res) => {
            this.$notify({
              title: '成功',
              message: '更新成功',
              type: 'success',
              duration: 2000
            })
            this.$emit('closeAction')
          })
        }else if(this.source === 'block'){
          await setBlockIsIntention(data).then((res) => {
            this.$notify({
              title: '成功',
              message: '更新成功',
              type: 'success',
              duration: 2000
            })
            this.$emit('closeAction')
          })
        }
      })
    }
  },
  watch: {
    show() {
      this.dialogVisible = this.show
    },
  }
}
</script>

<style scoped>

</style>

<!--

-->
